<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="@/assets/logo.png" alt="logo" />
            <div class="title">鱼图图云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto" style="padding-left: 20px">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div>
          <div v-if="loginUserState.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserState.loginUser.avatar" />
                <span class="username">{{ loginUserState.loginUser.username || '匿名' }}</span>
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/my_space">
                      <UserOutlined />
                      我的空间
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import {
  AppstoreOutlined,
  HomeOutlined,
  LogoutOutlined,
  PictureOutlined,
  PlusSquareOutlined,
  UsergroupAddOutlined,
  UserOutlined,
} from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserState } from '@/stores/useLoginUserState.ts'
import { logoutUsingPost } from '@/api/userController.ts'

const current = ref<string[]>([])
const originItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    icon: () => h(UsergroupAddOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    icon: () => h(PictureOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    icon: () => h(AppstoreOutlined),
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/add_pic',
    icon: () => h(PlusSquareOutlined),
    label: '创建图片',
    title: '创建图片',
  },
])

const router = useRouter()
// 过滤菜单项
const filterMenu = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserState.loginUser
      return loginUser && loginUser.userRole === 'admin'
    }
    return true
  })
}
const items = computed(() => filterMenu(originItems.value))
// 菜单点击事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}
// 路由切换后，更新菜单选中状态
router.afterEach((to) => {
  current.value = [to.path]
})

const loginUserState = useLoginUserState()
// 退出登录
const doLogout = async () => {
  await logoutUsingPost()
  message.success('已登出')
  // 清除登录用户信息
  loginUserState.setLoginUser({
    username: '未登录',
  })
  // 跳转到登录页
  await router.replace('/user/login')
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  width: 40px;
  height: 40px;
}

.title {
  font-size: 1.5rem;
  font-weight: bold;
  color: #5e91c1;
  margin-left: 10px;
}

.username {
  font-size: 20px;
}
</style>
